package com.zhiwang.planet.ui.my.withdraw;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import android.text.SpannedString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.zhiwang.planet.R;

import java.util.ArrayList;
import java.util.List;

public class WithDrawAdapter extends RecyclerView.Adapter<WithDrawAdapter.ViewHolder> {


    private List<String> datas = new ArrayList<>(4);
    private int prevPos = 0;
    private int currPos = 0;
    private Context context;

    OnItemSelectedListener onItemSelectedListener;

    public void setOnItemSelectedListener(OnItemSelectedListener onItemSelectedListener) {
        this.onItemSelectedListener = onItemSelectedListener;
    }

    public void setDatas(List<String> datas) {
        if (datas != null && !datas.isEmpty()) {
            if (this.datas.size() == 0) {
                this.datas.addAll(datas);
            } else {
                this.datas.clear();
                this.datas.addAll(datas);
            }

            notifyDataSetChanged();

        }
    }

    public void reset() {
        prevPos = currPos = 0;
        notifyDataSetChanged();
    }

    public void unselectAll() {
        prevPos = currPos = -1;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (context == null)
            context = parent.getContext();
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_withdraw_amount, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        holder.bind();
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }


    public int getSelectPos() {
        return currPos;
    }

    public void setSelected(TextView textView) {
        textView.setTextColor(ContextCompat.getColor(context, R.color.withdraw_select_color));
    }

    public void unSelect(TextView textView) {
        textView.setTextColor(ContextCompat.getColor(context, R.color.withdraw_select_color_nor));
    }

    public interface OnItemSelectedListener {
        void onItemSelected(int position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView textView;
        TextView unit;

        ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.withdraw_amount);
            unit = itemView.findViewById(R.id.withdraw_unit);
        }

        public void bind() {

            textView.setText(datas.get(getAdapterPosition()));
            if (getAdapterPosition() == currPos) {
                itemView.setBackgroundResource(R.drawable.withdraw_btn_enable);
                setSelected(textView);
                setSelected(unit);
            } else {
                itemView.setBackgroundResource(R.drawable.withdraw_btn_nor);
                unSelect(textView);
                unSelect(unit);
            }
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (getAdapterPosition() != currPos) {
                        prevPos = currPos;
                        currPos = getAdapterPosition();
                        if (prevPos >= 0 && prevPos <= getItemCount() - 1) {
                            FrameLayout prevFl = ((FrameLayout) ((ViewGroup) itemView.getParent()).getChildAt(prevPos));
                            AppCompatTextView prevTextView = prevFl.findViewById(R.id.withdraw_amount);
                            AppCompatTextView prevUnit = prevFl.findViewById(R.id.withdraw_unit);

                            unSelect(prevTextView);
                            unSelect(prevUnit);
                            prevFl.setBackgroundResource(R.drawable.withdraw_btn_nor);
                        }

                        setSelected(textView);
                        setSelected(unit);
                        itemView.setBackgroundResource(R.drawable.withdraw_btn_enable);
                    }
                    if (onItemSelectedListener != null)
                        onItemSelectedListener.onItemSelected(getAdapterPosition());
                }
            });
        }
    }
}
